/** Send new binder. Assumes we are already logged in */
function sendNewBinderCallback() {
	var box = document.getElementById("checkBoxes");
	var links = [];
	for ( var i = 0; i < box.childNodes.length; i++)
		if (box.childNodes[i].checked) {
			var ob = {
				"url" : box.childNodes[i].getAttribute(DATA_KEY),
				"title" : box.childNodes[i].getAttribute("label")
			};
			links.push(ob);
		}

	var title = document.getElementById("binderName");
	var favorite = document.getElementById("favorite");
	var tosend = {
		"id" : -1,
		"title" : title.value,
		"favorite" : favorite.checked,
		"links" : links
	};
	
	sendBinder(tosend, window.arguments[1]);
}

/**
 * On load of the "create new binder" dialog: create a set of checkboxes for
 * open tabs in the browser.
 */
function onLoad() {
	var dia = document.getElementById("createBinder");
	dia.centerWindowOnScreen();
	
	var box = document.getElementById("checkBoxes");
	clearChildren(box);
	// Populate with checkboxes for tabs
	var tabs = window.arguments[0];
	for ( var i = 0; i < tabs.length; i = i + 2) {
		// If URL is empty, skip
		if ("" == tabs[i+1] || "about:blank" == tabs[i+1])
			continue;
		var label = tabs[i];
		// If text is empty, use URL
		if ("" == label)
			label = tabs[i + 1].toString();
		if (label.length > 100)
			label = label.substr(0, 97) + "...";
		addNewElement(box, "checkbox", label, tabs[i + 1]);
	}
}

/** Set selection state on all check boxes to the given value */
function setItemsSelected(value) {
	var box = document.getElementById("checkBoxes");
	for ( var i = 0; i < box.childNodes.length; i++)
		box.childNodes[i].checked = value;
}